package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.product.client.ProductFeignClient;
import com.atguigu.gmall.product.client.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.model.product.SkuInfo;
import com.atguigu.gmall.product.client.model.product.SpuSaleAttr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/api/item")
public class ItemApiController {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;
    //数据汇总
    @GetMapping("/getItem/{skuId}")
    public Map<String,Object> getItem(@PathVariable Long skuId){
        Map<String,Object> result = new HashMap<>();
        //查询五部分数据

        CompletableFuture<SkuInfo> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            //1. 库存信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> completableFuture2 = completableFuture1.thenAcceptAsync(skuInfo -> {
            //2. 三级分类
            BaseCategoryView baseCategoryView =
                    productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);

        CompletableFuture<Void> completableFuture3 = CompletableFuture.runAsync(() -> {
            //3. 价格
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);

        CompletableFuture<Void> completableFuture4 = completableFuture1.thenAcceptAsync(skuInfo -> {
            //4. 销售及属性值
            List<SpuSaleAttr> saleAttrAndValueAndChecked =
                    productFeignClient.getSaleAttrAndValueAndChecked(skuId, skuInfo.getSpuId());
            result.put("spuSaleAttrList", saleAttrAndValueAndChecked);
        }, threadPoolExecutor);

        CompletableFuture<Void> completableFuture5 = completableFuture1.thenAcceptAsync(skuInfo -> {
            //5. 查询页面上 组合与skuId之间关系   //map集合转化为json串
            Map<String, String> skuValueIdsMap =
                    productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));
        }, threadPoolExecutor);

            //6.增加热度
        CompletableFuture.runAsync(() -> {
            listFeignClient.hotScore(skuId);
        }, threadPoolExecutor);

        //等待所有线程结束
        CompletableFuture.allOf(
                completableFuture1,
                completableFuture2,
                completableFuture3,
                completableFuture4,
                completableFuture5).join();
        
        return result;
    }
}
